// Optim.cpp

#include "../headers/Optim.h"
#include "../headers/CSingleton.h" 
#include "boost/algorithm/string/predicate.hpp"
#include "boost/algorithm/string/finder.hpp"
#include <boost/lexical_cast.hpp>

using namespace boost::algorithm;


void Optim::Process(Data<ifstream>& objInput,Data<ofstream>& objOutput)
{
	
	while(!objInput.Test_EOF()) //lecture des caractere jusqu'a End Of File	
	{
		_sTmp = objInput.Get_line();//renvoie ligne
		string t = Translate(_sTmp);
		objOutput.Write(t);
	}
}

string Optim::Translate(string t)
{
	string r;
	if (t != ""){
		int pos;
		pos=t.find(" ");
		//string test;
		//stringstream s;
		//s << int(pos);	
		//test = s.str() + " is the position of 'space' char\n";
	if ( boost::algorithm::istarts_with(t,"MOVE_LEFT") ){
			
		if(_countera!=0)	
		{
			string buffer;
		        buffer = t.substr(pos+1);
			if(_countera>0)
			{
				r="\nADD ";
				r+=boost::lexical_cast<string>(_countera);
			}else{
				r="\nSUB ";
				r+=boost::lexical_cast<string>(-_countera);
			}
			r+="\nMOVE_LEFT ";
			r+= buffer;
			_countera=0;
		}else{
			string buffer;
		        buffer = t.substr(pos+1);
			r="\nMOVE_LEFT ";
			r+=buffer;
		}
	
	} else if( boost::algorithm::istarts_with(t,"MOVE_RIGHT") ){
		
		if(_countera!=0)	
		{
			string buffer;
		        buffer = t.substr(pos+1);
			if(_countera>0)
			{
				r="\nADD ";
				r+=boost::lexical_cast<string>(_countera);
			}else{
				r="\nSUB ";
				r+=boost::lexical_cast<string>(-_countera);
			}
			r+="\nMOVE_RIGHT ";
			r+= buffer;
			_countera=0;
		}else{
			string buffer;
		        buffer = t.substr(pos+1);
			r="\nMOVE_RIGHT ";
			r+=buffer;
		}


	} else if (boost::algorithm::istarts_with(t,"ADD")) {

		if(_countera!=0)	
		{
			string buffer;
		        buffer = t.substr(pos+1);
			//r="\nADD ";
			//r+= buffer;
			istringstream buffers(buffer);
			int in;
			buffers >> in;
			_countera+=in;//(int)buffer;
		}else{
			string buffer;
		        buffer = t.substr(pos+1);
			//r="\nADD ";
			istringstream buffers(buffer);
			int in;
			buffers >> in;
			_countera+=in;
		}

	} else if(boost::algorithm::istarts_with(t,"SUB")) {

		if(_countera!=0)	
		{
			string buffer;
		        buffer = t.substr(pos+1);
			//r="\nADD ";
			//r+= buffer;
			istringstream buffers(buffer);
			int in;
			buffers >> in;
			_countera-=in;
		}else{
			string buffer;
		        buffer = t.substr(pos+1);
		//	r="\nSUB ";
			istringstream buffers(buffer);
			int in;
			buffers >> in;
			_countera-=in;
		}
			
	} else if (boost::algorithm::istarts_with(t,"START_LOOP")) {
		if(_countera!=0)	
		{
			if(_countera>0)
			{
				r="\nADD ";
				r+=boost::lexical_cast<string>(_countera);
			}else{
				r="\nSUB ";
				r+=boost::lexical_cast<string>(-_countera);
			}
			r+="\nSTART_LOOP ";
			_countera=0;
		}else{
			r="\nSTART_LOOP ";
		}

	} else	if (boost::algorithm::istarts_with(t,"END_LOOP"))
	{
		if(_countera!=0)	
		{
			if(_countera>0)
			{
				r="\nADD ";
				r+=boost::lexical_cast<string>(_countera);
			}else{
				r="\nSUB ";
				r+=boost::lexical_cast<string>(-_countera);
			}
			r+="\nEND_LOOP ";
			_countera=0;

		}else{
			r="\nEND_LOOP ";
		}
	} else	if (boost::algorithm::istarts_with(t,"SHOW")) {
		if(_countera!=0)	
		{
			string buffer;
		        buffer = t.substr(pos+1);
			if(_countera>0)
			{
				r="\nADD ";
				r+=boost::lexical_cast<string>(_countera);
			}else{
				r="\nSUB ";
				r+=boost::lexical_cast<string>(-_countera);
			}
			r+="\nSHOW ";
			_countera=0;

		}else{
			r="\nSHOW ";
		}
	} else 	if (boost::algorithm::istarts_with(t,"GET")) {
		if(_countera!=0)	
		{
			string buffer;
		        buffer = t.substr(pos+1);
			if(_countera>0)
			{
				r="\nADD ";
				r+=boost::lexical_cast<string>(_countera);
			}else{
				r="\nSUB ";
				r+=boost::lexical_cast<string>(-_countera);
			}
			r+="\nGET ";
			_countera=0;

		}else{
			r="\nGET ";
		}
	} else {
			r="";
	}
	
	return r;
	
	} else {
		return "";
	}
}

